package site.brood.auth.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.brood.common.core.ip.IpUtil;
import site.brood.common.core.str.StrUtil;
import site.brood.common.core.web.ServletUtil;
import site.brood.common.core.constant.Constants;
import site.brood.common.core.exception.ServiceException;
import site.brood.common.log.pojo.LogLoginDto;
import site.brood.common.core.user.LoginUserVO;
import site.brood.common.core.user.UserStatus;
import site.brood.common.core.web.AjaxJson;
import site.brood.common.log.service.LogService;
import site.brood.common.security.util.SecurityUtil;

@Service
public class ValidateService {

    @Autowired
    private LogService logService;

    public void checkLoginParams(String username, String password){
        String msgUsername = checkUsername(username);
        if(StrUtil.isNotEmpty(msgUsername)){
            recordLoginLog(username, false, "用户名不能为空");
            throw new ServiceException(msgUsername);
        }

        String msgPassword = checkPassword(password);
        if(StrUtil.isNotEmpty(msgPassword)){
            recordLoginLog(username, false, "密码不能为空");
            throw new ServiceException(msgPassword);
        }
    }

    public void checkRegisterParams(String username, String password){
        String msgUsername = checkUsername(username);
        if(StrUtil.isNotEmpty(msgUsername)){
            throw new ServiceException(msgUsername);
        }

        String msgPassword = checkPassword(password);
        if(StrUtil.isNotEmpty(msgPassword)){
            throw new ServiceException(msgPassword);
        }
    }

    public void checkLoginResult(String username,String password,AjaxJson<LoginUserVO> result){
        if (null == result.getData())
        {
            recordLoginLog(username, false, "登录用户不存在");
            throw new ServiceException("登录用户：" + username + " 不存在");
        }
        LoginUserVO user = result.getData();
        if (UserStatus.DELETED.getCode().equals(user.getDeleted()))
        {
            recordLoginLog(username, false, "对不起，您的账号已被删除");
            throw new ServiceException("对不起，您的账号：" + username + " 已被删除");
        }
        if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
        {
            recordLoginLog(username, false, "用户已停用，请联系管理员");
            throw new ServiceException("对不起，您的账号：" + username + " 已停用");
        }
        if (!SecurityUtil.matchesPassword(password, user.getPassword()))
        {
            recordLoginLog(username, false, "用户密码错误");
            throw new ServiceException("用户不存在/密码错误");
        }
    }

    /**
     * 检查注册用户返回数据
     * @param result
     */
    public void checkRegisterResult(AjaxJson result){
        AjaxJson.checkResponse(result, "brood-user");
    }

    public String checkUsername(String username){
        // 用户名或密码为空 错误
        if (StrUtil.isAnyBlank(username)) {
            return "用户/密码必须填写";
        }

        if (username.length() < Constants.USERNAME_MIN_LENGTH
                || username.length() > Constants.USERNAME_MAX_LENGTH)
        {
            return "用户名长度必须在"+Constants.USERNAME_MIN_LENGTH+"到"+Constants.USERNAME_MAX_LENGTH+"个字符之间";
        }

        return "";
    }

    public String checkPassword(String password){
        if (password.length() < Constants.PASSWORD_MIN_LENGTH
                || password.length() > Constants.PASSWORD_MAX_LENGTH)
        {
            return "密码长度必须在"+Constants.PASSWORD_MIN_LENGTH+"到"+Constants.PASSWORD_MAX_LENGTH+"个字符之间";
        }

        return "";
    }

    /**
     * 记录登录日志
     *
     * @param username 用户名
     * @param isSuccess 是否成功
     * @param message 消息内容
     * @return
     */
    public void recordLoginLog(String username, Boolean isSuccess, String message)
    {
        LogLoginDto loginLog = new LogLoginDto();
        loginLog.setUserName(username);
        loginLog.setIpaddr(IpUtil.getIpAddr(ServletUtil.getRequest()));
        loginLog.setMsg(message);
        // 日志状态
        if (isSuccess)
        {
            loginLog.setStatus("成功");
        }
        else{
            loginLog.setStatus("失败");
        }
        //把登陆日志写入数据库
        logService.loginLogAdd(loginLog);
    }
}
